package com.douyang.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.douyang.base.utils.IdWorkerUtils;
import com.douyang.dto.CreateVideoDTO;
import com.douyang.dto.GetReviewListDTO;
import com.douyang.dto.UpdateVideoDTO;
import com.douyang.base.enumeration.ReviewStatusEnum;
import com.douyang.base.expcetion.VideoException;
import com.douyang.utils.ESUtils;
import com.douyang.vo.VideoAndImageUrlVO;
import com.douyang.mapper.ReviewVideoInfoMapper;
import com.douyang.mapper.VideoBaseInfoMapper;
import com.douyang.base.model.PageResult;
import com.douyang.base.model.UserBaseInfo;
import com.douyang.pojo.ReviewVideoInfo;
import com.douyang.pojo.VideoBaseInfo;
import com.douyang.pojo.VideoInfo;
import com.douyang.pojo.VideoUploadCache;
import com.douyang.service.ReviewVideoInfoService;
import com.douyang.service.VideoInfoService;
import com.douyang.service.VideoUploadCacheService;
import com.douyang.utils.MinioUtil;
import com.douyang.base.utils.UserUtil;
import com.douyang.vo.ReviewVideoInfoVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xiaoma
 * @since 2025-02-27
 */
@Service
public class ReviewVideoInfoServiceImpl extends ServiceImpl<ReviewVideoInfoMapper, ReviewVideoInfo> implements ReviewVideoInfoService {


    @Autowired
    VideoUploadCacheService videoUploadCacheService;

    @Autowired
    VideoBaseInfoMapper videoBaseInfoMapper;

    @Autowired
    VideoInfoService videoInfoService;

    @Autowired
    MinioUtil minioutil;

    @Autowired
    ESUtils esUtils;

    @Override
    @Transactional
    public void createVideo(CreateVideoDTO createVideoDTO) {
        UserBaseInfo loginUserBaseInfo = UserUtil.getLoginUserBaseInfo();

        ReviewVideoInfo reviewVideoInfo = ReviewVideoInfo.builder()
                .id(IdWorkerUtils.getInstance().nextId())
                .title(createVideoDTO.getTitle())
                .typeName(createVideoDTO.getTypeName())
                .authorId(loginUserBaseInfo.getId())
                .authorName(loginUserBaseInfo.getUserName())
                .status(ReviewStatusEnum.WAITING)
                .createTime(LocalDateTime.now())
                .build();

        save(reviewVideoInfo);

        VideoInfo videoInfo=VideoInfo.builder()
                .videoId(reviewVideoInfo.getId())
                .videoMd5(createVideoDTO.getVideoMd5())
                .videoUrl(minioutil.getBasePath(createVideoDTO.getVideoMd5())+createVideoDTO.getVideoMd5()+".mp4")
                .imgUrl(createVideoDTO.getImageUrl())
                .build();

        videoInfoService.save(videoInfo);

        videoUploadCacheService.remove(
                new QueryWrapper<VideoUploadCache>()
                        .eq("file_md5",createVideoDTO.getVideoMd5())
        );
    }

    @Override
    public PageResult<ReviewVideoInfoVO> getNotListVideoInfo(GetReviewListDTO getReviewListDTO)  {
        UserBaseInfo loginUserBaseInfo = UserUtil.getLoginUserBaseInfo();
        Page<ReviewVideoInfo> page = getReviewListDTO.getPage();

        page=lambdaQuery()
                .eq(ReviewVideoInfo::getAuthorId, loginUserBaseInfo.getId())
                .eq(getReviewListDTO.getStatus()!=null,ReviewVideoInfo::getStatus, getReviewListDTO.getStatus())
                .page(page)
        ;


        return PageResult.of(page,notListVideoInfo -> {
            ReviewVideoInfoVO notListVideoInfoVO=new ReviewVideoInfoVO();
            BeanUtils.copyProperties(notListVideoInfo,notListVideoInfoVO);
            return notListVideoInfoVO;
        });
    }

    @Override
    public PageResult<ReviewVideoInfoVO> getReviewListByReviewer(GetReviewListDTO getReviewListDTO) {
        Page<ReviewVideoInfo> page = lambdaQuery()
                .eq(getReviewListDTO.getStatus() != null, ReviewVideoInfo::getStatus, getReviewListDTO.getStatus())
                .page(getReviewListDTO.getPage());
        return PageResult.of(page,reviewVideoInfo -> {
            ReviewVideoInfoVO reviewVideoInfoVO=new ReviewVideoInfoVO();
            BeanUtils.copyProperties(reviewVideoInfo,reviewVideoInfoVO);
            return reviewVideoInfoVO;
        });
    }

    @Override
    @Transactional
    public void updateVideo(UpdateVideoDTO updateVideoDTO) {
//        UserBaseInfo loginUserBaseInfo = UserUtil.getLoginUserBaseInfo();
        ReviewVideoInfo reviewVideoInfo = getById(updateVideoDTO.getId());
        if(reviewVideoInfo==null){throw new VideoException("无此id");}

        //之前不成功现在成功
        if(updateVideoDTO.getStatus().equals(ReviewStatusEnum.SUCCESS) && !reviewVideoInfo.getStatus().equals(ReviewStatusEnum.SUCCESS)){
            VideoBaseInfo videoBaseInfo=new VideoBaseInfo();
            BeanUtils.copyProperties(updateVideoDTO,videoBaseInfo);
            videoBaseInfo.setDeleted(false);
            videoBaseInfo.setLikeCount(0);
            videoBaseInfoMapper.insert(videoBaseInfo);
            try {
                esUtils.insertVideoBase(videoBaseInfo);
            }catch (Exception e){
                throw new VideoException(e.getMessage());
            }

        }
        //之前成功现在失败
        if(updateVideoDTO.getStatus().equals(ReviewStatusEnum.FAIL) && reviewVideoInfo.getStatus().equals(ReviewStatusEnum.SUCCESS)){
            videoBaseInfoMapper.deleteById(updateVideoDTO.getId());
            try {
                esUtils.deleteVideoBaseById(updateVideoDTO.getId());
            }catch (Exception e){
                throw new VideoException(e.getMessage());
            }

        }

        //更新review
        BeanUtils.copyProperties(updateVideoDTO,reviewVideoInfo);
        reviewVideoInfo.setFailOrPassTime(LocalDateTime.now());
        updateById(reviewVideoInfo);

    }

    @Override
    public VideoAndImageUrlVO getVideoAndImageUrl(Long id) {
        VideoInfo videoInfo= videoInfoService.lambdaQuery().eq(VideoInfo::getVideoId, id).one();
        if(videoInfo==null){throw new VideoException("不存在");}
        VideoAndImageUrlVO videoAndImageUrlVO=new VideoAndImageUrlVO();
        videoAndImageUrlVO.setImageUrl(videoInfo.getImgUrl());
        videoAndImageUrlVO.setVideoUrl(videoInfo.getVideoUrl());
        return videoAndImageUrlVO;
    }


}
